package com.zhg.javakc.modules.materials.controller;

import com.zhg.javakc.base.page.Page;
import com.zhg.javakc.base.util.CommonUtil;
import com.zhg.javakc.modules.materials.entity.MaterialsEntity;
import com.zhg.javakc.modules.materials.service.MaterialsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("masterials")
public class MaterialsController {
    @Autowired
   private MaterialsService materialsService;

    @RequestMapping("queryMasterials")
    public String queryMasterials(MaterialsEntity entity, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
        Page<MaterialsEntity> page=materialsService.queryMasterials(entity,request,response);
        modelMap.put("Page",page);
       return "masterials/list";
    }
    /**
     * 1.先做校验，校验请求中必须包含name是attachment的数据
     * 2.再把attachment交给CommonsMultipartFile
     * @RequestParam("attachment") MultipartFile gamPicture 方法参数括号里这么写
     * @RequestParam MultipartFile gamPicture  也可简写成这样
     */
    @RequestMapping("create")
    public String create(@RequestParam("gamPictureFile") MultipartFile gamPicture,MaterialsEntity materialsEntity){
        materialsEntity.setGamId(CommonUtil.uuid());
        File file =null;
       byte[] bytes=null;
        FileInputStream fis =null;
        try {
            String name = CommonUtil.uuid().toString();
            String path = "D:\\学习视频\\工作面试视频\\wll";
             file=new File(path + File.separator + name);
            gamPicture.transferTo(file);
            if (file!=null){
                 fis=new FileInputStream(file);
                if(fis!=null){
                 int len=fis.available();
                 bytes =new byte[len];
                 fis.read(bytes);
                 materialsEntity.setGamPicture(bytes);
                    materialsService.save(materialsEntity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "redirect:queryMasterials.do";
    }

    @RequestMapping("queryGrand")
    @ResponseBody
    public List<Map<String,Object>> queryGrand(String gamTypeId){
        List<Map<String,Object>> list = materialsService.queryGrand(gamTypeId);
        return list;
    }
    @RequestMapping("queryById")
    public String  queryById(String gamId,ModelMap map){
        MaterialsEntity materialsEntity=materialsService.get(gamId);
        map.put("entity",materialsEntity);
        return "masterials/update";
    }

    @RequestMapping("update")
    public String update(MaterialsEntity entity){
        materialsService.update(entity);
        return "redirect:queryMasterials.do";
    }
   @RequestMapping("delete")
    public String delete(String[] gamId){
       materialsService.delete(gamId);
        return "redirect:queryMasterials.do";
    }

    @RequestMapping("stop")
    public String updateStop(String gamId){
        materialsService.updateStop(gamId);
        return "redirect:queryMasterials.do";
    }

    @RequestMapping("start")
    public String updateStart(String gamId){
        materialsService.updateStart(gamId);
        return "redirect:queryMasterials.do";
    }

    @RequestMapping("queryByStatus")
    public String queryByStatus(MaterialsEntity entity, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
        Page<MaterialsEntity> page=materialsService.queryByStatus(entity,request,response);
        modelMap.put("Page",page);
        return "masterials/list";
    }
}



